import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_jpf_core/util/colors_util.dart';

import 'custom_text.dart';

class WidgetFatory
{
  static String _navBackImagePath = "";
  static String _imageEnterPath = "";

  static void config(String navBackImagePath,String imageEnterPath){
    _navBackImagePath = navBackImagePath;
    _imageEnterPath = imageEnterPath;
  }

  //创建标记值组件，如"姓名：XXX"
  static Widget createTagValView(String tag,String val,{double minRowHeight=30,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.center,Color tagColor=ColorsUtil.black_666,Color valColor=ColorsUtil.black_333,double tagWidth,TextStyle tagStyle,TextStyle valStyle}){
    return Container(constraints: BoxConstraints(minHeight: minRowHeight),child:  Row(
      crossAxisAlignment: crossAxisAlignment,children: [
      tagWidth == null?CustomText(tag,textColor: tagColor,style: tagStyle,):
      Container(width: tagWidth,child: CustomText(tag,textColor: tagColor,style: tagStyle),),
      Expanded(flex: 1,child: CustomText(val,textColor: valColor,style: valStyle,),)
    ],),);
  }

  //创建单行入口右边带">"，比如"我的运单"点击进入详情
  static Widget createRow(String title,{String imageEnterPath,bool showMust = false,Color titleColor=ColorsUtil.black_333,Widget centerView,Function action,double height=45,EdgeInsets padding,EdgeInsets margin,Color bgColor}){
    Widget mainView = Container(height: height,
      padding: padding == null?EdgeInsets.all(5):padding,
      margin: margin,
      alignment: Alignment.center,
      color: bgColor,
      child: centerView == null? Row(children: [
        showMust?CustomText("*",textSize: 18,textColor: ColorsUtil.red_ff606f,):Container(),
        Expanded(flex: 1,
          child:  CustomText(title,textColor: titleColor,),
        ),
        Image.asset(imageEnterPath??_imageEnterPath,fit: BoxFit.fill, width: 10,height: 20)],)
          :Row(children: [
        showMust?CustomText("*",textSize: 18,textColor: ColorsUtil.red_ff606f,):Container(),
        Expanded(flex: 1,
          child:  CustomText(title,textColor: titleColor,),
        ),
        centerView,
        Image.asset(imageEnterPath??_imageEnterPath,fit: BoxFit.fill, width: 10,height: 20)],),
    );
    return action == null? mainView :GestureDetector(onTap: action,child: mainView,);
  }

  ///生成一行可以点击的选择view，左右两边支持分别指定组件
  static Widget createSelectView(String value,{Color textColor = ColorsUtil.black_999,Widget leftView,Widget rightView,double height = 50,Function onClick}){
    return GestureDetector(
        onTap: onClick,
        child: Container(
          color: Colors.transparent,
          height: height,
          alignment: Alignment.center,
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              (leftView != null) ? leftView : Container(),
              Expanded(
                flex: 1,
                child: CustomText(value,textColor: textColor),
              ),
              (rightView != null) ? rightView : Container(),
            ],
          ),)
    );
  }

  static Widget createGrayLine({Color lineColor = ColorsUtil.grey_line,double lineHeight = 1,EdgeInsetsGeometry margin}){
    return Container(
      height: lineHeight,
      color: lineColor,
      margin: margin,
    );
  }


  static AppBar createMyAppBar(String title,BuildContext context,{
    Color textColor: ColorsUtil.black_333,
    Color bgColor: Colors.white,
    double textSize = 18,
    bool centerTitle = true,
    List<Widget> actions,
    bool hasLeftIcon = true,
    Brightness brightness,
    Widget titleView,
    double elevation = 0.5
  }){
      return  AppBar(
        title: titleView ?? Text(title,
            style: TextStyle(
                color: textColor,
                fontSize: textSize,
                fontWeight: FontWeight.bold
            )
        ),
        centerTitle:centerTitle,
        actions:actions,
        backgroundColor:bgColor,
        elevation: elevation,
        brightness: brightness == null?Brightness.light:brightness,
        leading: hasLeftIcon ? _getAppBarLeftIcon(context,bgColor) : Container(),
      );
  }


  static Widget _getAppBarLeftIcon(BuildContext context,Color bgColor,{String backImagePath}){
    return GestureDetector(
      onTap: (){
        Navigator.pop(context);
      },
      child: Container(
        width: 50,
        color: bgColor,
        padding: EdgeInsets.only(left: 10),
        alignment: Alignment.centerLeft,
        child: Image.asset(backImagePath??_navBackImagePath,fit: BoxFit.fill,width: 12,height: 22,),
      ),
    );
  }

  ///生成tab标签，可设置圆角、边线、背景色
  static Widget createLableChip(String text,Color bgColor,
      {Color textColor = Colors.white,
        double textSize = 12,
        Color lineColor = Colors.transparent,
        double radius = 99,
        EdgeInsetsGeometry padding,
        double width,
        double height,
      }){
    return Container(
      padding: padding == null ? EdgeInsets.fromLTRB(10, 1, 10, 1) : padding,
      width: width,
      height: height,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        border: Border.all(color: lineColor, width: 1.0),
        borderRadius: BorderRadius.circular(radius),
        color: bgColor,
      ),
      child: Text(text,style: TextStyle(fontSize: textSize,color: textColor)),
    );
  }
}